home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Examples / DemoText / UTEDocument.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  33.3 KB  |  1,199 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UTEDocument.cp 
  3. // Copyright © 1990-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UTEDOCUMENT__
  7. #include "UTEDocument.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __GEOMETRY__
  13. #include "Geometry.h"
  14. #endif
  15.  
  16. #ifndef __UAPPLICATION__
  17. #include "UApplication.h"
  18. #endif
  19.  
  20. #ifndef __UERRORMGR__
  21. #include "UErrorMgr.h"
  22. #endif
  23.  
  24. #ifndef __UFAILURE__
  25. #include "UFailure.h"
  26. #endif
  27.  
  28. #ifndef __UFILE__
  29. #include "UFile.h"
  30. #endif
  31.  
  32. #ifndef __ULIST__
  33. #include "UList.h"
  34. #endif
  35.  
  36. #ifndef __UMACAPPGLOBALS__
  37. #include "UMacAppGlobals.h"
  38. #endif
  39.  
  40. #ifndef __UMACAPPUTILITIES__
  41. #include "UMacAppUtilities.h"
  42. #endif
  43.  
  44. #if qPowerTalk
  45.     #ifndef __UMAILER__
  46.     #include "UMailer.h"
  47.     #endif
  48. #endif
  49.  
  50. #ifndef __UMEMORY__
  51. #include "UMemory.h"
  52. #endif
  53.  
  54. #ifndef __UMENUMGR__
  55. #include "UMenuMgr.h"
  56. #endif
  57.  
  58. #ifndef __UPATCH__
  59. #include "UPatch.h"
  60. #endif
  61.  
  62. #if UsingDemoTextPrinting
  63. #    ifndef __UPRINTING__
  64. #    include "UPrinting.h"
  65. #    endif
  66. #endif
  67.  
  68. #ifndef __UTECOMMANDS__
  69. #include "UTECommands.h"
  70. #endif
  71.  
  72. #ifndef __UTEVIEW__
  73. #include "UTEView.h"
  74. #endif
  75.  
  76. #ifndef __UVIEWSERVER__
  77. #include "UViewServer.h"
  78. #endif
  79.  
  80. #ifndef __UVIEW__
  81. #include "UView.h"
  82. #endif
  83.  
  84. #ifndef __UWINDOW__
  85. #include "UWindow.h"
  86. #endif
  87.  
  88. // Toolbox
  89.  
  90. #ifndef __ALIASES__
  91. #include <Aliases.h>
  92. #endif
  93.  
  94. #ifndef __BALLOONS__
  95. #include <Balloons.h>
  96. #endif
  97.  
  98. #ifndef __DIALOGS__
  99. #include <Dialogs.h>
  100. #endif
  101.  
  102. #ifndef __EDITIONS__
  103. #include <Editions.h>
  104. #endif
  105.  
  106. #ifndef __FONTS__
  107. #include <Fonts.h>
  108. #endif
  109.  
  110. #ifndef __MENUS__
  111. #include <Menus.h>
  112. #endif
  113.  
  114. #if qPowerTalk
  115.     #ifndef __OCESTANDARDMAIL__
  116.     #include <OCEStandardMail.h>
  117.     #endif
  118. #endif
  119.  
  120. #ifndef __PACKAGES__
  121. #include <Packages.h>
  122. #endif
  123.  
  124. #ifndef __PICKER__
  125. #include <Picker.h>
  126. #endif
  127.  
  128. #ifndef __RESOURCES__
  129. #include <Resources.h>
  130. #endif
  131.  
  132. #ifndef __TOOLUTILS__
  133. #include <ToolUtils.h>
  134. #endif
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // static data members
  138. //----------------------------------------------------------------------------------------
  139. TextSpecs TTEDocument::gDefaultSpecs;
  140.  
  141.  
  142. //========================================================================================
  143. // GLOBAL Procedures
  144. //========================================================================================
  145.  
  146.  
  147. //----------------------------------------------------------------------------------------
  148. // InitUTEDocument: 
  149. //----------------------------------------------------------------------------------------
  150. #pragma segment TEInit
  151.  
  152. void InitUTEDocument()
  153. {
  154.     TTEDocument::gDefaultSpecs.theTextFont = "Geneva";
  155.     TTEDocument::gDefaultSpecs.theTextFace = 0;
  156.     TTEDocument::gDefaultSpecs.theTextSize = 12;
  157.     TTEDocument::gDefaultSpecs.theTextColor = gRGBBlack;
  158.     TTEDocument::gDefaultSpecs.theJustification = teFlushDefault;
  159.     TTEDocument::gDefaultSpecs.theBackColor = gRGBWhite;
  160. } // InitUTEDocument 
  161.  
  162.  
  163. //========================================================================================
  164. // CLASS TTEDocument
  165. //========================================================================================
  166. #undef Inherited
  167.  
  168. #if qPowerTalk
  169. #define Inherited TMailableDocument
  170. #else
  171. #define Inherited TFileBasedDocument
  172. #endif
  173.  
  174. #pragma segment TEDocOpen
  175. MA_DEFINE_CLASS_M1(TTEDocument,Inherited);
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // TTEDocument::TTEDocument: 
  179. //----------------------------------------------------------------------------------------
  180. #pragma segment TEDocOpen
  181.  
  182. TTEDocument::TTEDocument()
  183. {
  184.     fDocText = NULL;
  185.     fElements = NULL;
  186.     fStyles = NULL;
  187.     fTEView = NULL;
  188.     fTextSpecs = gDefaultSpecs;
  189.     fViewResourceID = kDefaultViewID;
  190. #if qPowerTalk
  191.     fSendFormats = kSMPNativeMask + kSMPStandardInterchangeMask;
  192. #endif
  193. } // TTEDocument::TTEDocument 
  194.  
  195. //----------------------------------------------------------------------------------------
  196. // TTEDocument::ITEDocument: 
  197. //----------------------------------------------------------------------------------------
  198. #pragma segment TEDocOpen
  199.  
  200. void TTEDocument::ITEDocument(TFile* itsFile,
  201.                                      const OSType itsScrapType,
  202.                                      ResNumber itsViewResourceID)
  203. {
  204. #if qPowerTalk
  205.     this->IMailableDocument(itsFile, itsScrapType);
  206. #else
  207.     this->IFileBasedDocument(itsFile, itsScrapType);
  208. #endif
  209.  
  210.  
  211.  
  212.     FailInfo fi;
  213.     Try(fi)
  214.     {
  215.         fDocText = NewPermHandle(0);
  216.         fi.Success();
  217.     }
  218.     else
  219.     {
  220.         this->Free();
  221.         fi.ReSignal();
  222.     }
  223.     
  224.     fViewResourceID = itsViewResourceID;
  225. } // TTEDocument::ITEDocument 
  226.  
  227. //----------------------------------------------------------------------------------------
  228. // TTEDocument destructor
  229. //----------------------------------------------------------------------------------------
  230. #pragma segment TEDocClose
  231.  
  232. TTEDocument::~TTEDocument()
  233. {
  234.     fDocText = DisposeIfHandle(fDocText);
  235. }
  236.  
  237. //----------------------------------------------------------------------------------------
  238. // TTEDocument::ChangeBackColor: 
  239. //----------------------------------------------------------------------------------------
  240. #pragma segment TEDocNonRes
  241.  
  242. void TTEDocument::ChangeBackColor(const CRGBColor& newColor)
  243. {
  244.     GrafPtr oldPort;
  245.     TView * itsRootView;
  246.  
  247.     //??? Is this right?  Should we set the background color even if
  248.     // the view isn't in a window yet?
  249.     GetPort(&oldPort);
  250.     SetPort((*fTEView->fHTE)->inPort);
  251.     RGBBackColor(newColor);
  252.     itsRootView = fTEView->GetRootView();
  253.     if (itsRootView)
  254.         itsRootView->ForceRedraw();
  255.     SetPort(oldPort);
  256. } // TTEDocument::ChangeBackColor 
  257.  
  258. //----------------------------------------------------------------------------------------
  259. // TTEDocument::DoInitialState: 
  260. //----------------------------------------------------------------------------------------
  261. #pragma segment TEDocOpen
  262.  
  263. void TTEDocument::DoInitialState()    // Override 
  264. {
  265.     fTextSpecs = gDefaultSpecs;
  266. } // TTEDocument::DoInitialState 
  267.  
  268. //----------------------------------------------------------------------------------------
  269. // TTEDocument::DoMakeViews: 
  270. //----------------------------------------------------------------------------------------
  271. #pragma segment TEDocOpen
  272.  
  273. void TTEDocument::DoMakeViews(Boolean /*forPrinting*/ )    // Override 
  274.  
  275. {
  276.     TWindow* aWindow;
  277.  
  278.     FailNIL(aWindow = gViewServer->NewTemplateWindow(fViewResourceID, this));
  279.  
  280.     fTEView = (TTEView*) aWindow->FindSubView('TEVW');    // Must cast because FindSubView returns TView
  281.  
  282. #if UsingDemoTextPrinting
  283.     TStdPrintHandler* aHandler = new TStdPrintHandler;
  284.     aHandler->IStdPrintHandler(this,            // its document 
  285.                                fTEView,            // its view 
  286.                                kFixedSize,        // horzontal page size is fixed 
  287.                                !kFixedSize);    // vertical page size is variable (could be
  288.     // set to true on non-style TE systems)
  289. #endif
  290.  
  291.     this->ShowReverted();
  292. } // TTEDocument::DoMakeViews 
  293.  
  294. //----------------------------------------------------------------------------------------
  295. // TTEDocument::DoJustChange: 
  296. //----------------------------------------------------------------------------------------
  297. #pragma segment TEDocSelCommand
  298.  
  299. void TTEDocument::DoJustChange(CommandNumber aCommandNumber)
  300. {
  301.     short newJust;
  302.     TJustCommand * aJustChange;
  303.  
  304.     switch (aCommandNumber)
  305.     {
  306.         case cJustSystem:
  307.             newJust = teFlushDefault;
  308.             break;
  309.             
  310.         case cJustForceLeft:
  311.             newJust = teFlushLeft;
  312.             break;
  313.             
  314.         case cJustCenter:
  315.             newJust = teCenter;
  316.             break;
  317.             
  318.         case cJustRight:
  319.             newJust = teFlushRight;
  320.             break;
  321.     };
  322.     aJustChange = new TJustCommand;
  323.     aJustChange->IJustCommand(fTEView, newJust);
  324.     fTextSpecs.theJustification = newJust;
  325.     this->PostCommand(aJustChange);
  326. } // TTEDocument::DoJustChange 
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // TTEDocument::DoStyleChange: 
  330. //----------------------------------------------------------------------------------------
  331. #pragma segment TEDocSelCommand
  332.  
  333. void TTEDocument::DoStyleChange(CommandNumber aCommandNumber)
  334. {
  335.     TextStyle newStyle;
  336.  
  337.     switch (aCommandNumber)
  338.     {
  339.         case cBold:
  340.             newStyle.tsFace = bold;
  341.             break;
  342.             
  343.         case cItalic:
  344.             newStyle.tsFace = italic;
  345.             break;
  346.             
  347.         case cUnderline:
  348.             newStyle.tsFace = underline;
  349.             break;
  350.             
  351.         case cOutline:
  352.             newStyle.tsFace = outline;
  353.             break;
  354.             
  355.         case cShadow:
  356.             newStyle.tsFace = shadow;
  357.             break;
  358.             
  359.         case cCondense:
  360.             newStyle.tsFace = condense;
  361.             break;
  362.             
  363.         case cExtend:
  364.             newStyle.tsFace = extend;
  365.             break;
  366.     }
  367.     if ((newStyle.tsFace * fTextSpecs.theTextFace) == newStyle.tsFace)
  368.         fTextSpecs.theTextFace -= newStyle.tsFace;
  369.     else
  370.         fTextSpecs.theTextFace += newStyle.tsFace;
  371.     this->PostCommand(fTEView->DoMakeStyleCommand(newStyle, cStyleChange, doFace + doToggle));
  372. } // TTEDocument::DoStyleChange 
  373.  
  374. //----------------------------------------------------------------------------------------
  375. // TTEDocument::DoColTextChange: 
  376. //----------------------------------------------------------------------------------------
  377. #pragma segment TEDocSelCommand
  378.  
  379. void TTEDocument::DoColTextChange()
  380. {
  381.     TextStyle newStyle;
  382.     CRGBColor aColor;
  383.     CStr255 promptString;
  384.  
  385.     aColor = fTextSpecs.theTextColor;
  386.     GetIndString(promptString, kPromptsRsrcID, kColTextPrompt);
  387.     if (GetColor(kBestSystemLocation, promptString, aColor, ((CRGBColor &) newStyle.tsColor)))
  388.     {
  389.         fTextSpecs.theTextColor = newStyle.tsColor;
  390.         this->PostCommand(fTEView->DoMakeStyleCommand(newStyle, cColorChange, doColor + doToggle));
  391.     }
  392. } // TTEDocument::DoColTextChange 
  393.  
  394. //----------------------------------------------------------------------------------------
  395. // TTEDocument::DoColBackChange: 
  396. //----------------------------------------------------------------------------------------
  397. #pragma segment TEDocSelCommand
  398.  
  399. void TTEDocument::DoColBackChange()
  400. {
  401.     TextStyle newStyle;
  402.     CRGBColor aColor;
  403.     CStr255 promptString;
  404.  
  405.     aColor = fTextSpecs.theBackColor;
  406.     GetIndString(promptString, kPromptsRsrcID, kColBackPrompt);
  407.     if (GetColor(kBestSystemLocation, promptString, aColor, ((CRGBColor &) newStyle.tsColor)))
  408.     {
  409.         fTextSpecs.theBackColor = newStyle.tsColor;
  410.         this->ChangeBackColor(newStyle.tsColor);
  411.     }
  412. } // TTEDocument::DoColBackChange 
  413.  
  414. //----------------------------------------------------------------------------------------
  415. // TTEDocument::InstallChangedDeterminer: 
  416. //----------------------------------------------------------------------------------------
  417. #pragma segment TEDocSelCommand
  418.  
  419. void TTEDocument::InstallChangedDeterminer(VHSelect vhs,
  420.                                            SizeDeterminer sd)
  421. {
  422.     if (sd != fTEView->fSizeDeterminer[vhs])
  423.     {
  424.         fTEView->fSizeDeterminer[vhs] = sd;
  425.  
  426.         // If we changed the horizontal size determiner,
  427.         //  we must ask the TTEView to recompute the TE rectangles.
  428.         if (vhs == hSel)
  429.             switch (sd)
  430.             {
  431.                 case sizeSuperView:
  432.                     {
  433.                         VRect itsFrame(fTEView->GetFrame());
  434.                         fTEView->SuperViewChangedFrame(itsFrame, itsFrame, TRUE);// Don't care about deltas
  435.                         break;
  436.                     }
  437.  
  438.                 case sizePage:
  439.                     fTEView->DoPagination();
  440.                     break;
  441.             }
  442.  
  443.         fTEView->AdjustFrame();
  444.         fTEView->ForceRedraw();
  445.     }
  446. } // TTEDocument::InstallChangedDeterminer 
  447.  
  448. //----------------------------------------------------------------------------------------
  449. // TTEDocument::DoWidthChange: 
  450. //----------------------------------------------------------------------------------------
  451. #pragma segment TEDocSelCommand
  452.  
  453. void TTEDocument::DoWidthChange(CommandNumber aCommandNumber)
  454. {
  455.     SizeDeterminer sd;
  456.  
  457.     if (aCommandNumber == cWidthSuperView)
  458.         sd = sizeSuperView;
  459.     else if (aCommandNumber == cWidthOnePage)
  460.         sd = sizePage;
  461.     else
  462.         sd = sizeFixed;
  463.  
  464.     // NB: The following is not undoable in the
  465.     // current version
  466.     this->InstallChangedDeterminer(hSel, sd);
  467. } // TTEDocument::DoWidthChange 
  468.  
  469. //----------------------------------------------------------------------------------------
  470. // TTEDocument::DoHeightChange: 
  471. //----------------------------------------------------------------------------------------
  472. #pragma segment TEDocSelCommand
  473.  
  474. void TTEDocument::DoHeightChange(CommandNumber aCommandNumber)
  475. {
  476.     SizeDeterminer sd;
  477.  
  478.     switch (aCommandNumber)
  479.     {
  480.         case cHeightSuperView:
  481.             sd = sizeSuperView;
  482.             break;
  483.             
  484.         case cHeightPages:
  485.             sd = sizeFillPages;
  486.             break;
  487.             
  488.         case cHeightText:
  489.             sd = sizeVariable;
  490.             break;
  491.             
  492.         case cHeightConst:
  493.             sd = sizeFixed;
  494.             break;
  495.     }
  496.  
  497.     // NB: The following is not undoable in the
  498.     // current version
  499.     this->InstallChangedDeterminer(vSel, sd);
  500. } // TTEDocument::DoHeightChange 
  501.  
  502. //----------------------------------------------------------------------------------------
  503. // TTEDocument::DoMenuCommand: 
  504. //----------------------------------------------------------------------------------------
  505. #pragma segment TEDocSelCommand
  506.  
  507. void TTEDocument::DoMenuCommand(CommandNumber aCommandNumber)// override 
  508. {
  509.     CStr255 aName;
  510.     short menu;
  511.     short item;
  512.     TextStyle newStyle;
  513.  
  514.     CommandToMenuItem(aCommandNumber, menu, item);
  515.  
  516.     if (menu == mFont)
  517.     {
  518.         GetMenuItemText(MAGetMenu(menu), item, aName);
  519.         GetFNum(aName, &(newStyle.tsFont));
  520.         fTextSpecs.theTextFont = aName;
  521.         this->PostCommand(fTEView->DoMakeStyleCommand(newStyle, cFontChange, doFont + doToggle));
  522.     }
  523.     else if ((aCommandNumber > cSizeBase) && (aCommandNumber <= cSizeMax))
  524.     {
  525.         newStyle.tsSize = (short)(aCommandNumber - cSizeBase);
  526.         fTextSpecs.theTextSize = newStyle.tsSize;
  527.         this->PostCommand(fTEView->DoMakeStyleCommand(newStyle, cSizeChange, doSize + doToggle));
  528.     }
  529.     else
  530.         switch (aCommandNumber)
  531.         {
  532.             case cSizeOther:
  533.                 // as yet unsupported
  534.                 break;
  535.  
  536.             case cSizeGrow:
  537.             case cSizeShrink:
  538.                 if (aCommandNumber == cSizeGrow)
  539.                 {
  540.                     newStyle.tsSize = kRelSizeAmount;
  541.                     fTextSpecs.theTextSize += kRelSizeAmount;
  542.                 }
  543.                 else
  544.                 {
  545.                     newStyle.tsSize = -kRelSizeAmount;
  546.                     fTextSpecs.theTextSize -= kRelSizeAmount;
  547.                 }
  548.                 this->PostCommand(fTEView->DoMakeStyleCommand(newStyle, cSizeChange, doSize + addSize + doToggle));
  549.                 break;
  550.     
  551.             case cJustForceLeft:
  552.             case cJustCenter:
  553.             case cJustRight:
  554.             case cJustSystem:
  555.                 this->DoJustChange(aCommandNumber);
  556.                 break;
  557.     
  558.             case cPlainText:
  559.                 newStyle.tsFace = 0;
  560.                 fTextSpecs.theTextFace = 0;
  561.                 this->PostCommand(fTEView->DoMakeStyleCommand(newStyle, cStyleChange, doFace));
  562.                 break;
  563.     
  564.             case cBold:
  565.             case cItalic:
  566.             case cUnderline:
  567.             case cOutline:
  568.             case cShadow:
  569.             case cCondense:
  570.             case cExtend:
  571.                 this->DoStyleChange(aCommandNumber);
  572.                 break;
  573.     
  574.             case cColorText:
  575.                 this->DoColTextChange();
  576.                 break;
  577.     
  578.             case cColorBackground:
  579.                 this->DoColBackChange();
  580.                 break;
  581.     
  582.             case cWidthSuperView:
  583.             case cWidthView:
  584.             case cWidthOnePage:
  585.                 this->DoWidthChange(aCommandNumber);
  586.                 break;
  587.     
  588.             case cHeightSuperView:
  589.             case cHeightPages:
  590.             case cHeightText:
  591.             case cHeightConst:
  592.                 this->DoHeightChange(aCommandNumber);
  593.                 break;
  594.     
  595.             default:
  596.                 Inherited::DoMenuCommand(aCommandNumber);
  597.                 break;
  598.         }
  599. } // TTEDocument::DoMenuCommand 
  600.  
  601. //----------------------------------------------------------------------------------------
  602. // TTEDocument::DoNeedDiskSpace: 
  603. //----------------------------------------------------------------------------------------
  604. #pragma segment TEDocWriteFile
  605.  
  606. void TTEDocument::DoNeedDiskSpace(TFile* itsFile,
  607.                                          long& dataForkBytes,
  608.                                          long& rsrcForkBytes)// override 
  609. {
  610.  
  611.     dataForkBytes += GetHandleSize(fDocText);
  612.  
  613.     if ((fTEView->fStyleType == kWithStyle) && (fTEView->fHTE))
  614.     {
  615.         TEStyleHandle styles;
  616.         STHandle elements;
  617.         fTEView->ExtractStyles(styles, elements);
  618.         rsrcForkBytes += GetHandleSize((Handle)styles) + kRsrcTypeOverhead + kRsrcOverhead + GetHandleSize((Handle)elements) + kRsrcTypeOverhead + kRsrcOverhead;
  619.     }
  620.  
  621.     rsrcForkBytes += sizeof(TextSpecs) + kRsrcTypeOverhead + kRsrcOverhead;
  622.  
  623.     // Get resource file overhead 
  624.     Inherited::DoNeedDiskSpace(itsFile, dataForkBytes, rsrcForkBytes);
  625. } // TTEDocument::DoNeedDiskSpace 
  626.  
  627. //----------------------------------------------------------------------------------------
  628. // TTEDocument::DoRead: 
  629. //----------------------------------------------------------------------------------------
  630. #pragma segment TEDocReadFile
  631.  
  632. void TTEDocument::DoRead(TFile* aFile,
  633.                                 Boolean forPrinting)        // override 
  634. {
  635.     long numChars;
  636.     TextSpecsHdl hTextSpecs;
  637.     Boolean oldPermAllocation;
  638.     FailInfo fi;
  639.  
  640.     FailOSErr(aFile->GetDataLength(numChars));    // Read in the text 
  641.  
  642.     if (numChars > kUnlimited)                    // The file may have been created by… 
  643.     {
  644.         // …someone else - limit it to 32K! 
  645.         gApplication->ShowError(0, messageAlert + kFileTooBig);
  646.         numChars = kUnlimited;
  647.     }
  648.  
  649.     SetPermHandleSize(fDocText, numChars);
  650.     SignedByte savedState = LockHandleHigh(fDocText);    // lock handle before deref
  651.  
  652.     Try(fi)
  653.     {
  654.         FailOSErr(aFile->ReadData(*fDocText, numChars));
  655.         
  656.         HSetState(fDocText, savedState);        // restore state of handle
  657.         fi.Success();
  658.     }
  659.     else
  660.     {
  661.         HSetState(fDocText, savedState);        // restore state of handle
  662.         fi.ReSignal();
  663.     }
  664.  
  665.     // Read in the TEStyleRec 
  666.     fStyles = (TEStyleHandle)Get1Resource(kTextStyleRsrcType, kStylesRsrcID);
  667.     if (fStyles)
  668.         DetachResource((Handle)fStyles);
  669.  
  670.     // Read in the STElement array from permanent allocation 
  671.     oldPermAllocation = PermAllocation(TRUE);
  672.     fElements = (STHandle)(Get1Resource(kTextStyleRsrcType, kElementsRsrcID));
  673.     PermAllocation(oldPermAllocation);
  674.     
  675.     if (fElements)
  676.         DetachResource((Handle)fElements);
  677.  
  678.     // Read the text specs resource 
  679.     hTextSpecs = (TextSpecsHdl)Get1Resource(kTextSpecsRsrcType, kTextSpecsRsrcID);
  680.     if (hTextSpecs)
  681.     {
  682.         fTextSpecs = **hTextSpecs;
  683.         ReleaseResource((Handle)hTextSpecs);
  684.     }
  685.     else
  686.         this->DoInitialState();
  687.  
  688.     Inherited::DoRead(aFile, forPrinting);
  689. } // TTEDocument::DoRead 
  690.  
  691. #if qPowerTalk
  692. //----------------------------------------------------------------------------------------
  693. // TTEDocument::AddStandardMailContent: 
  694. //----------------------------------------------------------------------------------------
  695. #pragma segment TEDocRes
  696.  
  697. void TTEDocument::AddStandardMailContent(Boolean& okToSend)
  698. {
  699.     WindowRef theWindow = fTEView->GetGrafPort();
  700.     Boolean appendFlag = FALSE;
  701.     Handle theText = fTEView->ExtractText();
  702.     SignedByte wasTextState = LockHandleHigh(theText);
  703.     Ptr buffer = *theText;
  704.     unsigned long bufferSize = GetHandleSize(theText);
  705.     StScrpHandle theStyles = (StScrpHandle)NewHandle(10);
  706.     fTEView->GivePasteData((Handle)theStyles, 'styl');
  707.     SignedByte wasStyleState = LockHandleHigh((Handle)theStyles);
  708.     Boolean startNewScript = TRUE;
  709.     short scriptID = smRoman;
  710.     FailOSErr(SMPAddContent(theWindow, kMailStyledTextSegmentType, appendFlag,
  711.                             buffer, bufferSize, *theStyles, startNewScript, scriptID));
  712.     HSetState(theText, wasTextState);
  713.     HSetState((Handle)theStyles, wasStyleState);
  714.     DisposeIfHandle((Handle)theStyles);
  715.     okToSend = TRUE;
  716. }
  717.  
  718. //----------------------------------------------------------------------------------------
  719. // TTEDocument::ReadStandardMailContent: 
  720. //----------------------------------------------------------------------------------------
  721. #pragma segment TEDocRes
  722.  
  723. Boolean TTEDocument::ReadStandardMailContent()
  724. {
  725.     Boolean readContent = FALSE;
  726.     const unsigned long kBufferSize = 10000;
  727.     MailSegmentMask segmentTypeMask = kMailStyledTextSegmentMask;
  728.     Handle buffer                     = NewHandle(kBufferSize);
  729.     FailNIL(buffer);
  730.     
  731.     const short kNumStyles          = 200;
  732.     const long  kStyleBufferSize = (sizeof(ScrpSTElement) * kNumStyles) + 4;
  733.     StScrpHandle theStyles          = (StScrpHandle)NewHandleClear(kStyleBufferSize);
  734.     FailNIL(theStyles);
  735.     
  736.     Boolean endOfContent         = FALSE;
  737.     OSErr err                     = noErr;
  738.     Boolean allContentExtracted = TRUE;
  739.     
  740.     while ((err == noErr) && !endOfContent)
  741.     {
  742.         // skip past empty text segments
  743.         ScriptCode         script             = 0;
  744.         unsigned long    dataSize        = 0;
  745.         long             segmentLength     = 0;
  746.         long             segmentID         = 0;
  747.         Boolean         endOfScript     = FALSE;
  748.         Boolean         endOfSegment     = FALSE;
  749.         MailSegmentType segmentType;
  750.         
  751.         endOfContent = FALSE;
  752.  
  753.     
  754.         unsigned long bufferSize = kBufferSize;
  755.         SetHandleSize(buffer, bufferSize);
  756.         SetHandleSize((Handle)theStyles, kStyleBufferSize);
  757.         (**theStyles).scrpNStyles = kNumStyles;
  758.         SignedByte wasStylesState = LockHandle((Handle)theStyles);
  759.         SignedByte wasBufferState = LockHandle(buffer);
  760.         err = SMPReadContent(fLetter->GetMailerWindowRef(), segmentTypeMask,
  761.                              *buffer, bufferSize, &dataSize, *theStyles, &script,
  762.                              &segmentType, &endOfScript, &endOfSegment, &endOfContent,
  763.                              &segmentLength, &segmentID);
  764.         
  765.         if(allContentExtracted && (dataSize > 0) && !endOfContent)
  766.             allContentExtracted = FALSE;
  767.         
  768.         if ((err == noErr) && (dataSize > 0))
  769.         {
  770.             HSetState(buffer, wasBufferState);
  771.             HSetState((Handle)theStyles, wasStylesState);
  772.             SetHandleSize(buffer, dataSize);
  773.             unsigned long textSize = dataSize;
  774.             dataSize = (sizeof(ScrpSTElement) * (**theStyles).scrpNStyles) + 4;
  775.             SetHandleSize((Handle)theStyles, dataSize);
  776.             TEStyleInsert(*buffer, textSize, (StScrpHandle)theStyles, fTEView->fHTE);
  777.             fTEView->SynchView(FALSE);
  778.             readContent = TRUE;
  779.         }
  780.     }
  781.     
  782.     DisposeIfHandle(buffer);
  783.     DisposeIfHandle((Handle)theStyles);
  784.     
  785.     if(!allContentExtracted)
  786.     {
  787.         MacAppAlert(kContentNotExtracted, NULL);
  788.     }
  789.  
  790.     return readContent;
  791. }
  792.  
  793. //----------------------------------------------------------------------------------------
  794. // TTEDocument::ReadNativeMailContent: 
  795. //----------------------------------------------------------------------------------------
  796. #pragma segment TEDocRes
  797.  
  798. Boolean TTEDocument::ReadNativeMailContent()
  799. {    
  800.     Boolean readContent = TMailableDocument::ReadNativeMailContent();
  801.     
  802.     // fStyles and fElements are native to DemoText (see TTEDocument::DoRead)
  803.     // so they can be used as a valid check for native content.
  804.     if (fDocText && fStyles && fElements)
  805.     {
  806.         this->ShowReverted();
  807.         readContent = TRUE;
  808.     }
  809.     return readContent;
  810. }
  811.  
  812. //----------------------------------------------------------------------------------------
  813. // TTEDocument::ReadNativeMailContent: 
  814. //----------------------------------------------------------------------------------------
  815. #pragma segment TEDocRes
  816.  
  817. Boolean TTEDocument::ReadSnapshotMailContent()
  818. {    
  819.     Boolean readContent = FALSE;
  820.     
  821.     OCECreatorType             theBlockType;
  822.     theBlockType.msgCreator = 'apml';
  823.     theBlockType.msgType     = 'imag';
  824.     unsigned long             blockIndex = 1;
  825.     const unsigned long     theBufferSize = 10000;
  826.     Handle                    buffer = NewHandle(theBufferSize);
  827.     FailNIL(buffer);
  828.     
  829.     unsigned long            dataOffset     = 0;
  830.     unsigned long            dataSize     = 0;
  831.     Boolean                    endOfBlock     = FALSE;
  832.     unsigned long            dataRemaining;
  833.     OSErr                     err = noErr;
  834.     
  835.     unsigned long bufferSize = theBufferSize;
  836.     SetHandleSize(buffer, bufferSize);
  837.  
  838.     
  839.     err = SMPReadBlock(fLetter->GetMailerWindowRef(),&theBlockType,blockIndex,*buffer,bufferSize,
  840.                         dataOffset,&dataSize,&endOfBlock,&dataRemaining);               
  841.     if(err == noErr && dataSize > 0)
  842.     {
  843.         MacAppAlert(kSnapshotNotExtracted, NULL);
  844.         readContent = TRUE;
  845.     }
  846.     
  847.     return readContent;
  848. }
  849.  
  850. //----------------------------------------------------------------------------------------
  851. // TTEDocument::SetReplyContents: 
  852. //----------------------------------------------------------------------------------------
  853. #pragma segment TEDocRes
  854.  
  855. void TTEDocument::SetReplyContents(TDocument* replyToDoc)
  856. {
  857.     DisposeIfHandle(fDocText);
  858.     fDocText = ((TTEDocument*)replyToDoc)->fDocText;
  859.     FailOSErr(HandToHand(&fDocText));
  860.     
  861.     DisposeIfHandle((Handle)fElements);
  862.     fElements = ((TTEDocument*)replyToDoc)->fElements;
  863.     if (fElements != NULL)
  864.         FailOSErr(HandToHand((Handle*)&fElements));
  865.     
  866.     DisposeIfHandle((Handle)fStyles);
  867.     fStyles = ((TTEDocument*)replyToDoc)->fStyles;
  868.     if (fStyles != NULL)
  869.         FailOSErr(HandToHand((Handle*)&fStyles));
  870.     
  871.     ShowReverted();
  872.     
  873.     if (fTEView->Focus())
  874.     {
  875.         (*fTEView->fHTE)->selStart = 0;
  876.         (*fTEView->fHTE)->selEnd = 0;
  877.         
  878.         CStr255 replySpacer;
  879.         GetIndString(replySpacer, kPromptsRsrcID, kReplySpacer);
  880.         TextStyle newStyle;
  881.         newStyle.tsFace = 0;
  882.         TESetStyle(doFace,&newStyle,FALSE,fTEView->fHTE);
  883.         TEInsert((Ptr)&replySpacer.fStr[1], replySpacer.Length(),fTEView->fHTE);
  884.         
  885.         (*fTEView->fHTE)->selStart = 0;
  886.         (*fTEView->fHTE)->selEnd = 0;
  887.         fTEView->SynchView(FALSE);
  888.     }
  889. }
  890.  
  891. //----------------------------------------------------------------------------------------
  892. // TTEDocument::ImageDocumentForLetter: 
  893. //----------------------------------------------------------------------------------------
  894. #pragma segment TEDocRes
  895.  
  896. void TTEDocument::ImageDocumentForLetter(Boolean /* inColor */)
  897. {
  898.     // ••• Need to fill this in •••
  899. }
  900.  
  901. #endif // qPowerTalk
  902.  
  903. //----------------------------------------------------------------------------------------
  904. // TTEDocument::DoSetupMenus: 
  905. //----------------------------------------------------------------------------------------
  906. #pragma segment TEDocRes
  907.  
  908. void TTEDocument::DoSetupMenus()        // override 
  909. {
  910.     Boolean checkPlain;
  911.     Boolean checkFont;
  912.     Boolean specChange;
  913.     short just;
  914.     short aMode;
  915.     Style aFace;
  916.     SizeDeterminer sd;
  917.     MenuRef aMenuRef;
  918.     CStr255 aName;
  919.     TextStyle aStyle;
  920.     short theFont;
  921.     short startOfSelection;
  922.     short endOfSelection;
  923.     short nItems;
  924.     
  925.     Inherited::DoSetupMenus();
  926.  
  927.     startOfSelection = (*fTEView->fHTE)->selStart;
  928.     endOfSelection = (*fTEView->fHTE)->selEnd;
  929.  
  930.     aMode = doFace;
  931.     checkPlain = fTEView->ContinuousStyle(startOfSelection, endOfSelection, aMode, aStyle) && (aStyle.tsFace == 0);
  932.  
  933.     aMode = doAll;
  934.     aStyle.tsFace = bold | italic | underline | outline | shadow | extend | condense;
  935.  
  936.     specChange = fTEView->ContinuousStyle(startOfSelection, endOfSelection, aMode, aStyle);
  937.     checkFont = ((aMode & doFont) != 0);
  938.  
  939.     aMenuRef = MAGetMenu(mFont);
  940.     if (aMenuRef)
  941.     {
  942.         // If specs have changed && EITHER an old-style record || the font's continuous - Ed. 
  943.         specChange = (fTEView->fSpecsChanged && checkFont);
  944.     
  945.         GetFontName(aStyle.tsFont, aName);            // Get real font number in case tsFont is 
  946.         GetFNum(aName, &theFont);                    // …the system or application font. 
  947.         nItems = CountMItems(aMenuRef);
  948.         for (short item = 1; item <= nItems; ++item)
  949.         {
  950.             // There can be more than 31 menu entries with scrolling menus, but trying to enable
  951.             // an item with number > 31 is bad news.  If the menu itself is enabled (which it
  952.             // will be in MacApp if any of the first 31 items is enabled), then the extras
  953.             // will always be enabled.
  954.             if (item <= 31)
  955.                 EnableItem(aMenuRef, item);
  956.             if (checkFont)
  957.             {
  958.                 short fnt;
  959.     
  960.                 GetMenuItemText(aMenuRef, item, aName);
  961.                 GetFNum(aName, &fnt);
  962.                 CheckItem(aMenuRef, item, (fnt == theFont));
  963.             }
  964.         }
  965.     }
  966.  
  967.     sd = fTEView->fSizeDeterminer[hSel];        // Enable size determiner related menu items
  968.     EnableCheck(cWidthSuperView, TRUE, (sd == sizeSuperView));
  969.     EnableCheck(cWidthOnePage, TRUE, (sd == sizePage));
  970.     EnableCheck(cWidthView, TRUE, (sd == sizeFixed));
  971.  
  972.     sd = fTEView->fSizeDeterminer[vSel];
  973.     EnableCheck(cHeightSuperView, TRUE, (sd == sizeSuperView));
  974.     EnableCheck(cHeightPages, TRUE, (sd == sizeFillPages));
  975.     EnableCheck(cHeightText, TRUE, (sd == sizeVariable));
  976.     EnableCheck(cHeightConst, TRUE, (sd == sizeFixed));
  977.  
  978.     just = fTEView->fJustification;                // Enable justification related menu items 
  979.     EnableCheck(cJustSystem, TRUE, (just == teFlushDefault));
  980.     EnableCheck(cJustForceLeft, TRUE, (just == teFlushLeft));
  981.     EnableCheck(cJustCenter, TRUE, (just == teCenter));
  982.     EnableCheck(cJustRight, TRUE, (just == teFlushRight));
  983.  
  984.     Enable(cStyle, TRUE);                        // Enable sub-menus 
  985.     Enable(cSize, TRUE);
  986.     Enable(cFont, TRUE);
  987.     Enable(cColor, (qNeedsColorQD || HasColorQD()));
  988.  
  989.     aFace = aStyle.tsFace;
  990.     EnableCheck(cPlainText, TRUE, checkPlain);    // Enable normal Style menu items 
  991.     EnableCheck(cBold, TRUE, bold & aFace);
  992.     EnableCheck(cItalic, TRUE, italic & aFace);
  993.     EnableCheck(cUnderline, TRUE, underline & aFace);
  994.     EnableCheck(cOutline, TRUE, outline & aFace);
  995.     EnableCheck(cShadow, TRUE, shadow & aFace);
  996.     EnableCheck(cCondense, TRUE, condense & aFace);
  997.     EnableCheck(cExtend, TRUE, extend & aFace);
  998.  
  999.     for (CommandNumber c = cSizeMin; c <= cSizeMax; ++c)
  1000.     {
  1001.         Boolean checkSize;
  1002.  
  1003.         if (!(aMode & doSize))
  1004.             checkSize = FALSE;
  1005.         else
  1006.             checkSize = ((c - cSizeBase) == aStyle.tsSize);
  1007.  
  1008.         EnableCheck(c, TRUE, checkSize);
  1009.         if (fTEView->fSpecsChanged)
  1010.         {
  1011.             // if it _is_ styled and font is continuous 
  1012.             // …and the size is a real one
  1013.             if ((aMode & doFont) && RealFont(aStyle.tsFont, (short) (c - cSizeBase)))
  1014.                 aFace = outline;                // …then we outline it 
  1015.             else
  1016.                 aFace = 0;
  1017.             SetStyle(c, aFace);
  1018.         }
  1019.     }
  1020.  
  1021.     Enable(cSizeGrow, TRUE);
  1022.     Enable(cSizeShrink, TRUE);
  1023.  
  1024.     Enable(cColorText, (qNeedsColorQD || HasColorQD()));
  1025.     Enable(cColorBackground, (qNeedsColorQD || HasColorQD()));
  1026.  
  1027.     fTEView->fSpecsChanged = FALSE;
  1028. } // TTEDocument::DoSetupMenus 
  1029.  
  1030. //----------------------------------------------------------------------------------------
  1031. // TTEDocument::DoWrite: 
  1032. //----------------------------------------------------------------------------------------
  1033. #pragma segment TEDocWriteFile
  1034.  
  1035. void TTEDocument::DoWrite(TFile* aFile,
  1036.                                  Boolean makingCopy)        // override 
  1037. {
  1038.     long numChars;
  1039.     TextSpecsHdl hTextSpecs;
  1040.  
  1041.     // Write out the text 
  1042.     numChars = GetHandleSize(fDocText);
  1043.     SignedByte savedState = LockHandleHigh(fDocText);    // lock handle before deref
  1044.     FailOSErr(aFile->WriteData(*fDocText, numChars));
  1045.     HSetState(fDocText, savedState);        // restore state of handle
  1046.  
  1047.     if (fTEView->fStyleType == kWithStyle)
  1048.     {
  1049.         TEStyleHandle styles;
  1050.         STHandle elements;
  1051.  
  1052.         fTEView->ExtractStyles(styles, elements);
  1053.         PermHandToHand((Handle &) styles);
  1054.         AddResource((Handle)styles, kTextStyleRsrcType, kStylesRsrcID, gEmptyString);
  1055.         FailResError();                        // !!! need a failure handler
  1056.         PermHandToHand((Handle &) elements);
  1057.         AddResource((Handle)elements, kTextStyleRsrcType, kElementsRsrcID, gEmptyString);
  1058.         FailResError();                        // !!! need a failure handler
  1059.     }
  1060.  
  1061.     // Write the text specification resource, after converting it to a handle 
  1062.     hTextSpecs = (TextSpecsHdl)NewPermHandle(sizeof(TextSpecs));
  1063.     **hTextSpecs = fTextSpecs;
  1064.     AddResource((Handle)hTextSpecs, kTextSpecsRsrcType, kTextSpecsRsrcID, gEmptyString);
  1065.     FailResError();                            // !!! need a failure handler
  1066.     
  1067.     Inherited::DoWrite(aFile, makingCopy);
  1068. } // TTEDocument::DoWrite 
  1069.  
  1070. //----------------------------------------------------------------------------------------
  1071. // TTEDocument::FreeData: 
  1072. //----------------------------------------------------------------------------------------
  1073. #pragma segment TEDocClose
  1074.  
  1075. void TTEDocument::FreeData()            // override 
  1076. {
  1077.     SetPermHandleSize(fDocText, 0);
  1078. } // TTEDocument::FreeData 
  1079.  
  1080. //----------------------------------------------------------------------------------------
  1081. // TTEDocument::SetSpecStyle: 
  1082. //----------------------------------------------------------------------------------------
  1083. #pragma segment TEDocRes
  1084.  
  1085. void TTEDocument::SetSpecStyle()
  1086. {
  1087.     short theFont;
  1088.     TextStyle theTS;
  1089.     CStr255 aStr255;
  1090.  
  1091.     aStr255 = fTextSpecs.theTextFont;
  1092.     GetFNum(aStr255, &theFont);
  1093.     CRGBColor theTextColor = fTextSpecs.theTextColor;
  1094.     SetTextStyle(theTS, theFont, fTextSpecs.theTextFace, fTextSpecs.theTextSize, theTextColor);
  1095.     fTEView->SetOneStyle(0, 0, doAll, theTS, kDontRedraw);
  1096. } // TTEDocument::SetSpecStyle 
  1097.  
  1098. //----------------------------------------------------------------------------------------
  1099. // TTEDocument::ShowReverted: 
  1100. //----------------------------------------------------------------------------------------
  1101. #pragma segment TEDocReadFile
  1102.  
  1103. void TTEDocument::ShowReverted()        // override 
  1104. {
  1105.     CRGBColor aColor;
  1106.  
  1107.     fTEView->StuffText(fDocText);                // put in the text 
  1108.     TESetSelect(0, 0, fTEView->fHTE);
  1109.     if (fStyles && fElements)                    // If we're able, stuff style info 
  1110.         fTEView->StuffStyles(fStyles, fElements);
  1111.     else
  1112.         this->SetSpecStyle();
  1113.  
  1114.     if (qNeedsColorQD || HasColorQD())
  1115.     {
  1116.         aColor = fTextSpecs.theBackColor;
  1117.         this->ChangeBackColor(aColor);
  1118.     }
  1119.     fTEView->SetJustification(fTextSpecs.theJustification, kDontRedraw);
  1120.     Inherited::ShowReverted();
  1121. } // TTEDocument::ShowReverted 
  1122.  
  1123.  
  1124. //========================================================================================
  1125. // CLASS TJustCommand
  1126. //========================================================================================
  1127.  
  1128.  
  1129. #undef Inherited
  1130. #define Inherited TCommand
  1131.  
  1132. #pragma segment TEDocSelCommand
  1133. MA_DEFINE_CLASS_M1(TJustCommand,Inherited);
  1134.  
  1135.  
  1136. //----------------------------------------------------------------------------------------
  1137. // TJustCommand::TJustCommand:
  1138. //----------------------------------------------------------------------------------------
  1139. #pragma segment TEDocSelCommand
  1140. TJustCommand::TJustCommand()
  1141. {
  1142.     fOldJust = teFlushDefault;
  1143.     fNewJust = teFlushDefault;
  1144.     fTEView = NULL;
  1145. } // TJustCommand::TJustCommand 
  1146.  
  1147. //----------------------------------------------------------------------------------------
  1148. // TJustCommand destructor
  1149. //----------------------------------------------------------------------------------------
  1150. #pragma segment MADestructorRes
  1151.  
  1152. TJustCommand::~TJustCommand()
  1153. {
  1154. }
  1155.  
  1156. //----------------------------------------------------------------------------------------
  1157. // TJustCommand::IJustCommand: 
  1158. //----------------------------------------------------------------------------------------
  1159. #pragma segment TEDocSelCommand
  1160.  
  1161. void TJustCommand::IJustCommand(TTEView* itsTEView,
  1162.                                        short itsNewJust)
  1163. {
  1164.     TCommandHandler* theContext = NULL;
  1165.  
  1166.     if (itsTEView)
  1167.         theContext = itsTEView->GetContext(cJustChange);
  1168.  
  1169.     this->ICommand(cJustChange, theContext, kCanUndo, kCausesChange, theContext);
  1170.     fTEView = itsTEView;
  1171.     fOldJust = itsTEView->fJustification;
  1172.     fNewJust = itsNewJust;
  1173. } // TJustCommand::IJustCommand 
  1174.  
  1175. //----------------------------------------------------------------------------------------
  1176. // TJustCommand::DoIt: 
  1177. //----------------------------------------------------------------------------------------
  1178. #pragma segment TEDocDoCommand
  1179.  
  1180. void TJustCommand::DoIt()            // override 
  1181. {
  1182.     fTEView->SetJustification(fNewJust, kRedraw);
  1183. } // TJustCommand::DoIt 
  1184.  
  1185. //----------------------------------------------------------------------------------------
  1186. // TJustCommand::UndoIt: 
  1187. //----------------------------------------------------------------------------------------
  1188. #pragma segment TEDocDoCommand
  1189.  
  1190. void TJustCommand::UndoIt()            // override 
  1191. {
  1192.     fTEView->SetJustification(fOldJust, kRedraw);
  1193. } // TJustCommand::UndoIt 
  1194.  
  1195. //----------------------------------------------------------------------------------------
  1196. // End of UTEDocument.cp
  1197.  
  1198. #pragma segment Inline
  1199.